<?php
require_once("resources/commons.php");
require_once("data/User.php");
require_once("HTML/QuickForm/Controller.php");
require_once("HTML/QuickForm/Action/Display.php");
require_once("HTML/QuickForm/Action/Submit.php");
require_once("HTML/QuickForm/text.php");
require_once("HTML/QuickForm/password.php");
require_once("HTML/QuickForm/hidden.php");
require_once("HTML/QuickForm/submit.php");
require_once("HTML/QuickForm/image.php");
require_once("Log.php");


/**
 * Registration form
 */
class RegisterPage extends HTML_QuickForm_Page{
    function buildForm(){
        $this->_formBuilt = true;

		$l_firstNameInput=new HTML_QuickForm_text("firstName");
		$l_firstNameInput->setLabel("First name :");
		$this->addElement($l_firstNameInput);
		$l_lastNameInput=new HTML_QuickForm_text("lastName");
		$l_lastNameInput->setLabel("Last name :");
		$this->addElement($l_lastNameInput);
		$l_passwordInput=new HTML_QuickForm_password("password");
		$l_passwordInput->setLabel("Password :");
		$this->addElement($l_passwordInput);
		$l_checkPasswordInput=new HTML_QuickForm_password("checkPassword");
		$l_checkPasswordInput->setLabel("Password (confirm) :");
		$this->addElement($l_checkPasswordInput);
		$l_captchaImage=new HTML_QuickForm_image("captchaImage");
		$l_captchaImage->setSource("resources/captcha.php");
		$this->addElement($l_captchaImage);
		$l_captchaCheckInput=new HTML_QuickForm_text("captchaCheck");
		$l_captchaCheckInput->setLabel("Insert the validation code :");
		$this->addElement($l_captchaCheckInput);
		$l_hiddenProfileUpdateKey=new HTML_QuickForm_hidden("profileUpdateKey");
		$l_hiddenProfileUpdateKey->setValue($_REQUEST[PROFILE_UPDATE_KEY]);
		$this->addElement($l_hiddenProfileUpdateKey);
		
		$l_submit=new HTML_QuickForm_submit("submit");
		$l_submit->setValue("OK");
		$this->addElement($l_submit);
		
		
		//The validation rules (either client and server)
		$this->addRule("firstName","First name must not be empty","required",null,"client");
		$this->addRule("lastName","Last name must not be empty","required",null,"client");
		$this->addRule("password","Password must not be empty","required",null,"client");
		$this->addRule("checkPassword","Password confirmation must not be empty","required",null,"client");
		$this->addRule("captchaCheck","Please enter the validation code","required",null,"client");
		
		$this->addRule("firstName","First name must not be empty","required",null,"server");
		$this->addRule("lastName","Last name must not be empty","required",null,"server");
		$this->addRule("password","Password must not be empty","required",null,"server");
		$this->addRule("checkPassword","Password confirmation must not be empty","required",null,"server");
		$this->addRule("captchaCheck","Please enter the validation code","required",null,"server");
		$this->registerRule("validateCaptcha","callback","validateCaptcha");
		$this->addRule("captchaCheck","Invalid validation code","validateCaptcha",null,"server");
		$this->addRule(array('password', 'checkPassword'), 'The passwords do not match', 'compare', null, 'client');
		
		$this->applyFilter("firstName","trim");
		$this->applyFilter("lastName","trim");
		$this->applyFilter("password","trim");
		$this->applyFilter("checkPassword","trim");
		
        $this->setDefaultAction("submit");
    }
}


/**
 * Quickform rule which check if the CAPTCHA
 * @param string $p_captchaCheck
 * @return boolean true if the rule is valid
 */
function validateCaptcha($p_captchaCheck){
	return $p_captchaCheck==$_SESSION[CAPTCHA_VALUE];
}

/**
 * We subclass the default 'display' handler to customize the output
 */
class ActionDisplay extends HTML_QuickForm_Action_Display{
    function _renderForm($page){    	
    	//Get the account associated with the profile update key
    	$l_profileUpdateKey=$page->exportValue("profileUpdateKey");
    	$l_record=User::getFromCustomCriteria("WHERE profileChangeKey=:profileChangeKey and isActive=:isActive",array("profileChangeKey" => $l_profileUpdateKey, "isActive" => false));
    	if(count($l_record)==0){
    		displayError("ERROR 1");
    	}else{
    		$l_user=array_shift($l_record);
	    	displayTop("Registration process",null,null);
	    	$page->setDefaults(array(
	    		"firstName" => $l_user->getFirstName(),
	    		"lastName" => $l_user->getLastName()
	    	));
	    	echo "<p>Please complete the registration form.</p>";
	        $page->display();
	        displayBottom();
    	}
    }
}

/**
 * Action to be called after a submit action.
 * Activate the account
 */
class ActionProcess extends HTML_QuickForm_Action{
    function perform($page){
    	//Update the user account
    	$l_profileUpdateKey=$page->exportValue("profileUpdateKey");
    	$l_record=User::getFromCustomCriteria("WHERE profileChangeKey=:profileChangeKey and isActive=:isActive",array("profileChangeKey" => $l_profileUpdateKey, "isActive" => false));
    	if(count($l_record)==0){
    		displayError("ERROR 4");
    	}else{
    		$l_user=array_shift($l_record);
    		$l_user->setFirstName($page->exportValue("firstName"));
    		$l_user->setLastName($page->exportValue("lastName"));
    		$l_user->setCredentials(sha1($page->exportValue("password")));
    		$l_user->setIsActive(true);
    		$l_user->save();
    		
	    	displayTop("Registration process",null,null);
	    	echo "<p>Account updated !</p>";
	    	echo "<a href=\"" . ROOT_PATH . "/apps/\">Click here to open a new session</a>";
	        displayBottom();
    	}
    }
}

$controller =new HTML_QuickForm_Controller('RegistrationProcess', false);
if(isset($_REQUEST[PROFILE_UPDATE_KEY])){
	$controller->container(true);
	$controller->setDefaults(array("profileUpdateKey" => $_REQUEST[PROFILE_UPDATE_KEY]));
}
$controller->addPage(new RegisterPage("registration"));
$controller->addAction('submit', new HTML_QuickForm_Action_Submit());
$controller->addAction('display', new ActionDisplay());
$controller->addAction('process', new ActionProcess());

$controller->run();
?>